import {observable, action, toJS} from 'mobx';
import {WeaTableNew, WeaForm} from 'comsMobx';
import {WeaLocaleProvider} from "ecCom";
import {message, Modal, Alert} from "antd";
import * as contractManagementApi from '../api/ContractManagementApi';
import {indexOf, isBlank, parseIntValue} from '../util/CommonUtil';


const confirm = Modal.confirm;
const error = Modal.error;
const {TableStore} = WeaTableNew;
const {getLabel} = WeaLocaleProvider;

/**
 * 模板store
 */
class InitConfigListStore {

    /**
     * 是否有操作页面的权限
     *
     * @type {boolean}
     */
    @observable authorized = true;

    /**
     * 加载状态
     */
    @observable loading = false;

    /**
     * 分页控件的store
     */
    @observable tableStore = new TableStore();

    /**
     * 获取初始化配置列表数据
     */
    @action
    getTableData = () => {
        this.loading = true;
        contractManagementApi.getInitConfigList().then(res => {
            this.loading = false;
            const {code, data} = res;
            if (code == 401) {
                this.authorized = false;
            } else if (code == 200) {
                if (data && data.sessionkey) {
                    this.tableStore.getDatas(data.sessionkey);
                }
            }
        });
    }

    /**
     * 一键初始化
     */
    @action
    affirmInitAll = () => {
        confirm({
            title: getLabel('518566', "信息确认"),
            content: getLabel('519656', "确定要初始化吗？"),
            okText: getLabel('518568', "是"),
            cancelText: getLabel('518569', "否"),
            onOk: () => this.doInitAll(),
            onCancel() {
            },
        });
    }

    /**
     * 一键初始化
     */
    @action
    doInitAll = () => {
        this.loading = true;
        contractManagementApi.initAll().then(res => {
            this.loading = false;
            const {code, notification} = res;
            if (code == 200) {
                message.success(notification == undefined ? getLabel("518560", "操作成功") : notification);
            } else if (code == 500 && notification != undefined) {
                var notifications = notification.split(";");
                const config = {
                    width: 550,
                    title: getLabel('518566', "信息确认"),
                    content: (
                        <div>
                            {this.createErrorNotificationContent(notification)}
                        </div>
                    ),
                };
                error(config);
            } else {
                message.error(getLabel("518561", "操作失败"));
            }
            this.getTableData();
        });
    }

    createErrorNotificationContent = (notification) => {
        var notifications = notification.split(";");
        var items = [];
        for (let i = 0; i < notifications.length; i++) {
            items.push(<p key={i}>{notifications[i]}</p>)
        }
        return items;
    }

}

export default InitConfigListStore;